/*
 * Copyright 2012 Fabian Wenzelmann
 * 
 * This file is part of Recognition-Procedures-for-Boolean-Functions.
 * 
 * Recognition-Procedures-for-Boolean-Functions is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Recognition-Procedures-for-Boolean-Functions is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Recognition-Procedures-for-Boolean-Functions.  If not, see <http://www.gnu.org/licenses/>.
 */

package de.wenzelmf.booleanFunction.booleanExpression;

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * A Boolean expression that is just a propositional variable.
 * 
 * @author Fabian Wenzelmann
 * @version 1.0
 * 
 */
public class Atom extends BooleanExpression
{
	/*
	 * Identifier (the propositional variable).
	 */
	private String identifier;
	
	/**
	 * Creates a variable with a given identifier (the name of the propositional variable).
	 * 
	 * @param identifier Name of the propositional variable.
	 */
	public Atom(String identifier)
	{
		this.identifier = identifier;
	}
	
	/**
	 * Simply returns the value assigned to this atom by <i>v</i>.
	 */
	@Override
	public boolean getTruthValue(Assignment alpha)
	{
		return alpha.getTruthValue(this);
	}
	
	/**
	 * Simply the identifier.
	 */
	public String toString()
	{
		return identifier;
	}
	
	/**
	 * The identifier (name of the propositional variable).
	 * 
	 * @return Name of the propositional variable
	 */
	public String getIdentifier()
	{
		return identifier;
	}
	
	/**
	 * The hash code of the atom is the hash code of the identifier.
	 */
	public int hashCode()
	{
		return getIdentifier().hashCode();
	}
	
	/**
	 * Returns <code>true</code> if <code>other</code> is also an Atom object and has
	 * the same identifier.
	 */
	@Override
	public boolean equals(Object other)
	{
		if(other == null || !(other instanceof Atom))
			return false;
		Atom otherAtom = (Atom)other;
		return getIdentifier().equals(otherAtom.getIdentifier());
	}
	
	@Override
	public BooleanFunction getFunction()
	{
		return new BooleanFunction()
		{

			@Override
			public boolean getValue(Iterator<Boolean> inputPoint)
			{
				return inputPoint.next();
			}

			@Override
			public int getDimension()
			{
				return 1;
			}
			
		};
	}
	
	// TODO correct?
	@Override
	public int getArity()
	{
		return 1;
	}

	@Override
	public Iterator<BooleanExpression> iterator()
	{
		return new Iterator<BooleanExpression>()
				{

					@Override
					public boolean hasNext()
					{
						return false;
					}

					@Override
					public BooleanExpression next()
					{
						throw new NoSuchElementException("Atomic expression does not have subformulae.");
					}

					@Override
					public void remove()
					{
						throw new UnsupportedOperationException("It is not allowed to remove elements from a formula.");
					}
				};
	}

	@Override
	public int completeSize()
	{
		return 1;
	}
	
}
